package tests;

import java.io.*;

import iterator.*;

import iterator.CondExpr;

import iterator.FldSpec;

import iterator.RelSpec;

import java.io.*;

import java.util.*;
import java.lang.*;
import heap.*;
import bufmgr.*;
import diskmgr.*;
import global.*;
import chainexception.*;

import columnar.*;
import index.*;

public class BatchInsert implements GlobalConst {

	/*
	 * constructor for all parameters - datafilename , coulmnDBname, columnar
	 * filename , numcolumns
	 */
	public String datafilename, cdbname, filename;
	public int numcolumns;
	public TID[] tidArr;
	ColumnarFile _cf;
	int[] colnosg;
	int[] attrtypesg;

	BatchInsert(String dfname, String cDBname, String fname, int ncoulmns) {
		this.datafilename = dfname;
		this.cdbname = cDBname;
		this.filename = fname;
		this.numcolumns = ncoulmns;
		colnosg = new int[this.numcolumns];
		attrtypesg = new int[this.numcolumns];

		AttrType[] type;

	}

	public void runTest() {
		TID tid;
		int m = 0;

		try {
			// System.out.println ("\n" + "Running " + testName() + " tests...."
			// + "\n");
			PCounter.initialize();
			String dbpath = "/tmp/" + this.cdbname;
			String record;
			int i = 0;
			SystemDefs sysdef = new SystemDefs(dbpath, 10000, 1000, "Clock");
			AttrType[] attr = new AttrType[this.numcolumns];
			ColumnarFile cf = new ColumnarFile();

			// File file = new File(this.datafilename);
			BufferedReader br = new BufferedReader(new FileReader("/tmp/"
					+ this.datafilename));
			while ((record = br.readLine()) != null) {
				if (i == 0) {
					StringTokenizer st = new StringTokenizer(record, "\t");
					BufferedWriter br1 = new BufferedWriter(new FileWriter(
							"/tmp/"+this.filename+".hdr"));
					int j = 0;
					while (st.hasMoreTokens()) {
						StringTokenizer st1 = new StringTokenizer(st
								.nextToken(), ":");
						String attrName = st1.nextToken().trim();
						String attrType = st1.nextToken().trim();
						if (attrType.equalsIgnoreCase("Int"))
							attr[j] = new AttrType(AttrType.attrInteger);
						else
							attr[j] = new AttrType(AttrType.attrString);
						br1.write(attrName + " " + (j + 1) + " "
								+ attr[j].attrType + "\n");
						j++;

					}
					cf = new ColumnarFile(this.filename, this.numcolumns, attr);
					br1.close();
				} else {
					StringTokenizer st2 = new StringTokenizer(record, "\t");
					String[] data = new String[this.numcolumns];
					int x = 0;
					while (st2.hasMoreTokens()) {
						data[x] = st2.nextToken();
						x++;
						// System.out.println(st.nextToken());
					}
					Tuple tup = new Tuple();
					int k = 0;
					for (AttrType at : attr) {
						if (at.attrType == 0)
							k++;
					}
					short[] tsize = new short[k];
					for (int l = 0; l < k; l++) {
						tsize[l] = strlen;
					}
					short num = (short) this.numcolumns;
					tup.setHdr(num, attr, tsize);
					Tuple t = new Tuple(tup.size());
					t.setHdr(num, attr, tsize);
					for (int y = 0; y < this.numcolumns; y++) {
						if (attr[y].attrType == 0)
							t.setStrFld(y + 1, data[y].trim());
						else
							t
									.setIntFld(y + 1, Integer.parseInt(data[y]
											.trim()));
					}
					// System.out.println(t.returnTupleByteArray().length);
					// System.out.println(Convert.getStrValue(8,t.returnTupleByteArray(),34));
					tid = cf.insertTuple(t.returnTupleByteArray());
			/*		t = cf.getTuple(tid);
					tup.tupleCopy(t);
					for (int y = 0; y < this.numcolumns; y++) {
						if (attr[y].attrType == 0)
							System.out.println(t.getStrFld(y + 1));
						else
							System.out.println(t.getIntFld(y + 1));
					}
*/
					// t.setHdr(num, attr, tsize);

					// code to update the entire tuple
				//	t.setIntFld(3, 100);

					// cf.updateTuple(tid, t);
					/*
					 * cf.updateColumnofTuple(tid, t, 3);
					 * 
					 * t = cf.getTuple(tid); tup.tupleCopy(t); //_cf=cf; for(int
					 * y=0;y<this.numcolumns;y++) { if(attr[y].attrType==0)
					 * System.out.println(t.getStrFld(y+1)); else
					 * System.out.println(t.getIntFld(y+1)); }
					 */

					// System.out.println(cf.getValue(tid,1));
					// System.out.println(tup.g)
					// this.tidArr[m] = tid;
					// m++;
					// for

				}

				i++;

			}

			// for(i=0;i<this.tidArr.length;i++)
			// cf.getTuple(this.tidArr[i]);
			// btree column number 1
			_cf = cf;
			//_cf.createBitMapIndex(, "dbName", value);
			// System.out.println("Btree key and pageid and slot no");
			// cf.createBTreeIndex(1);
		}

		catch (Exception e) {
			System.out.println(e.getMessage());
		} finally {
			try {
				// IntegerValue vc = new IntegerValue(new Integer(1));
				// cf.createBitMapIndex(2, (ValueClass)vc);
				// for(i=0;i<this.tidArr.length;i++)
				// cf.getTuple(this.tidArr[i]);
				// cf.markTupleDeleted(tid);
				// cf.purgeAllDeletedTuples();
				SystemDefs.JavabaseBM.unpinAllPages();
				SystemDefs.JavabaseBM.flushAllPages();
				System.out.println("Number of Pages Read: " + PCounter.rcounter);
				System.out.println("Number of Pages Written: "
						+ PCounter.wcounter);
			} catch (Exception e) {
			}
		}

	}

void test2(int columnNum, int attr)
{
 try {
	_cf.findDistinctValues(columnNum, attr)	;
} catch (Exception e) {
	System.out.println("Error in Bitmap Creation");
}
}


	public static void main(String args[]) throws IOException, HFException,
			HFBufMgrException, HFDiskMgrException {
		Scanner sc = new Scanner(System.in);
		String dbname, Colfilename, valcons, accesstype, datafile, valcolnam, operator, value;
		int numbuf, targetnum, nocol;

		// System.out.println("Enter The datafile,dbname,Colfilename,number of cols");
		// datafile=sc.next();
		// dbname=sc.next();
		// Colfilename=sc.next();
		// nocol=sc.nextInt();

		BatchInsert bi = new BatchInsert("datafile.txt", "cdbname", "hpfile", 4);

		// BatchInsert bi = new BatchInsert(datafile,dbname,Colfilename,nocol);
		bi.runTest();
		// index
//		 System.out.println(" do you want to choose index type: (y/n)");
		//System.out.println("Enter the Column name for Btree/Bitmap Construction");
		//sc = new Scanner(System.in);
		//String s = sc.nextLine();
		//System.out.println("Enter the AcessType of Btree/BitMap");
		//String acc = sc.nextLine();
		
		
//		bi.test2(colno, attr);

	}
}